
/*******************************************************************************
 * The code below this comment is integral part of the project "Jelliphy".
 * 
 * "Jelliphy" is a project created by: 
 * Dispage - Patrizio Gelosi
 * Via A. De Gasperi 91 
 * P. Potenza Picena (MC) - Italy
 * 
 * (Hereby referred to as "DISPAGE")
 * 
 * Copyright (c) 2011 DISPAGE.
 * 
 * @Released on: 02-06-2011
 * @Version: 1.0.00
 * @Build: 1109
 * @Site: http://www.dispage.com
 * @Contacts: http://www.dispage.com/contacts
 * 
 * The contents of this file are released under the GNU General Public License
 * version 3 as published by the Free Software Foundation that can be found at
 * the address "http://opensource.org/licenses/gpl-3.0.html". If the license
 * cannot be found, write to the Free Software Foundation, Inc., 51 Franklin
 * Street, Fifth Floor, Boston, MA 02110-1301 USA.
 * 
 * You may not use the present file except in compliance with the License.
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied.  See the License
 * for the specific language governing rights and limitations under the
 * License.
 * 
 * The interactive user interfaces in modified source and object code versions
 * of this program must display Appropriate Legal Notices, as required under
 * Section 5 of the GNU General Public License version 3.
 * 
 * In accordance with Section 7(b) of the GNU General Public License version 3,
 * these Appropriate Legal Notices must retain the display of the "Powered by
 * Dispage" logo. If the display of the logo is not reasonably feasible for
 * technical reasons, the Appropriate Legal Notices must display the words
 * "Powered by Dispage".
 * 
 ******************************************************************************/

var MGDial = {

	tmpText : '',
	tmpPar2 : null,

	optionIconMap : {
		error: '0 0',
		question: '0 -37px',
		warning: '0 -73px',
		info: '-36px 0'
	},

	openFloat : function ($parent) {

		var preAbs = $parent.css('position') != 'fixed';

		$parent.cssImportant('position', 'fixed');
		if (preAbs || JlQ.browser.msie || JlQ.browser.mozilla4 || JlQ.browser.opera) {
			$parent.css({
				top: (parseInt($parent.css('top')) - JlQ(document).scrollTop()) + 'px',
				left: (parseInt($parent.css('left')) - JlQ(document).scrollLeft()) + 'px'
			});
		}		
	},

	openDiag : function () {

		var $parent = JlQ(this).parent();

		JlQ('.mg-tut-buttons').remove();

		MGDial.openFloat($parent);

		JlQ('.JLui-widget-overlay').addClass('mg-nohighlight');
		JlQ('#mg-dialog .mg-dialogue-text').removeAttr('style');
	},

	closeOpenDialog : function (func, text, par2) {

		MGTut.setReopen();
		JlQ('#mg-dialog').dialog('close');
		this.tmpText = text;
		this.tmpPar2 = par2;
		setTimeout('MGDial.' + func + '(MGDial.tmpText, MGDial.tmpPar2);', 100);
	},

	warning : function (text, options) {
		
		if (JlQ('#mg-dialog:visible').length) {
			this.closeOpenDialog('warning', text, options);
			return;
		}
		options = JlQ.extend(options, {});

		var op = JlQ.extend(options.op, {});

		if (options.overCode) {
			text = MGLang.errorCodes[options.overCode];
		}
		if (!options.type) {
			options.type = 'Warning';
		}
		JlQ('#mg-dialog .mg-dialogue-text').html(text);
		JlQ('.mg-dialogue-icon').css('background-position', this.optionIconMap[options.type.toLowerCase()]);

		if (!op.close && typeof(MGTut) != 'undefined') op.close = MGTut.getReopen();

		this.dialog(JlQ.extend({
				title: options.type,
				buttons: { "Ok": options.close ? options.close : function() { JlQ(this).dialog("close"); }}	
			},
			op)
		);
	},

	confirm : function (text, yesCallback) {
		
		if (JlQ('#mg-dialog:visible').length) {
			this.closeOpenDialog('confirm', text, yesCallback);
			return;
		}
		JlQ('#mg-dialog .mg-dialogue-text').html(text);
		JlQ('.mg-dialogue-icon').css('background-position', this.optionIconMap.question);

		this.dialog({
			title: "Confirm",
			buttons: { 
				"No": function() { JlQ(this).dialog("close"); }, 
				"Yes": function() { JlQ(this).dialog("close"); yesCallback.call(); }
			},
			close: MGTut.getReopen()
		});
	},

	dialog : function (options) {

		JlQ('#mg-dialog').dialog(JlQ.extend({
				dialogClass: 'mg-nohighlight',
				modal: true, 
				show: null,
				hide: null,
				position: 'center',
				width: 300,
				height: 'auto',
				minHeight: 150,
				zIndex: 2000000,
				open: this.openDiag,
				close: null,
				buttons: {}	
			},
			options
		));
	}
}
